package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: dw
 * @Date 2022/3/9 14:42
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Autowired(required = false)
    private HttpSession session;

    /**
     * 用户登录
     *
     * @param employee
     * @return
     */
    @Override
    public Employee login(Employee employee) {
        //调用mapper层查询用户
        Employee loginEmployee = employeeMapper.login(employee);

        //判断用户是否存在
        if (loginEmployee==null) {
            throw new BusinessException("用户名或密码错误，请重试");
        }

        //校验密码
        //调用工具类获取加密密码
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!md5DigestAsHex.equals(loginEmployee.getPassword())) {
            throw new BusinessException("用户名或密码错误，请重试");
        }

        //判断用户是否被禁用
        if (loginEmployee.getStatus()==0) {
            throw new BusinessException("用户已被禁用，请联系管理员");
        }

        //登录成功将登录用户id上传到会话域
        session.setAttribute("employee",loginEmployee.getId());

        return loginEmployee;
    }

    /**
     * 条件分页查询
     *
     * @param page     当前页
     * @param pageSize 每页数据
     * @param name     条件
     * @return
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        //设置当前页和每页数据
        PageHelper.startPage(page,pageSize);
        //调用mapper查询
        List<Employee> employeeList = employeeMapper.findAll(name);
        //使用PageInfo封装
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);

        //使用Page封装
        Page<Employee> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());

        return pageObj;
    }

    /**
     * 添加用户
     *
     * @param employee
     * @return
     */
    @Override
    public boolean save(Employee employee) {
        //判断用户是否已经存在
        Employee loginEmployee = employeeMapper.login(employee);
        if (loginEmployee!=null) {
            throw new BusinessException("用户已存在，请重新添加");
        }

        //获取会话域中id
        Long id = (Long) session.getAttribute("employee");
        //补全数据
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(1);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(id);
        employee.setUpdateUser(id);

        return employeeMapper.save(employee)>0;
    }

    /**
     * 根据id回显页面
     *
     * @param id
     * @return
     */
    @Override
    public Employee findById(Long id) {
        return employeeMapper.findById(id);
    }

    /**
     * 修改用户
     *
     * @param employee
     * @return
     */
    @Override
    public boolean update(Employee employee) {
        //判断修改的用户名是否存在，存在则不让修改名字，并排除自己以外进行比较，选择自己则可以修改其他内容
        Employee loginEmployee = employeeMapper.login(employee);
        if (loginEmployee!=null && !employee.getId().equals(loginEmployee.getId())) {
                throw new BusinessException("该用户名已存在");
        }

        return employeeMapper.update(employee)>0;
    }



    /*@Override
    public boolean update(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        return employeeMapper.update(employee)>0;
    }*/


}
